#include "stdafx.h"
#include "AdpcmSessionManager.h"

//---------------------------GLOBAL VARIABLES-----------------------------------

static AdpcmSessionManager s_sessionManager;

//---------------------------DLL ENTRY POINT------------------------------------

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
					 )
{
	switch (ul_reason_for_call)
	{
	case DLL_THREAD_ATTACH:
		//CoInitialize(NULL);
		break;

	case DLL_PROCESS_ATTACH:
		CoInitialize(NULL);
		break;

	case DLL_THREAD_DETACH:
		//CoUninitialize();
		break;
	case DLL_PROCESS_DETACH:
		CoUninitialize();
#ifdef _DEBUG
		OutputDebugString("=====>Dump memory leak for Audio SDK.\n");
		_CrtDumpMemoryLeaks();
#endif
		break;
	}

	return TRUE;
}

//---------------------------ADPCM AUDIO SDK API--------------------------------

#define DLLSPEC extern "C" __declspec(dllexport)

DLLSPEC int __cdecl InitializeAudioSDK()
{
	return s_sessionManager.Initialize();
}

DLLSPEC void __cdecl UninitializeAudioSDK()
{
	s_sessionManager.Uninitialize();
}

DLLSPEC HAUDIOSESSION __cdecl StartPlaybackAudioEx(int channels, int sampleRate, int bitsPerSample, AudioCodecId codecId)
{
	return s_sessionManager.CreatePlaybackSession(channels, sampleRate, bitsPerSample, codecId);
}

DLLSPEC HAUDIOSESSION __cdecl StartPlaybackAudio(int channels, int sampleRate, int isAdpcmAudio)
{
	return StartPlaybackAudioEx(
								channels, 
								sampleRate,
								16,
								(isAdpcmAudio == 0) ? AUDIOCODECID_PCM : AUDIOCODECID_ADPCM);
}

DLLSPEC int __cdecl PlaybackAudioData(HAUDIOSESSION session, char *data, int len)
{
	return s_sessionManager.PlaybackAdpcmData(session, data, len);
}

DLLSPEC int __cdecl SetAudioPlaybackVolume(HAUDIOSESSION session, int volume)
{
	return s_sessionManager.SetAdpcmPlaybackVolume(session, volume);
}

DLLSPEC int __cdecl GetAudioPlaybackVolume(HAUDIOSESSION session)
{
	return s_sessionManager.GetAdpcmPlaybackVolume(session);
}

DLLSPEC void __cdecl StopPlaybackAudio(HAUDIOSESSION session)
{
	s_sessionManager.DestroySession(session);
}

DLLSPEC void __cdecl PausePlaybackAudio(HAUDIOSESSION session)
{
	s_sessionManager.PausePlayAdpcmData(session);
}

DLLSPEC void __cdecl ResumePlaybackAudio(HAUDIOSESSION session)
{
	s_sessionManager.ResumePlayAdpcmData(session);
}

DLLSPEC HAUDIOSESSION __cdecl StartCaptureAudioEx(int deviceIndex, int channels, int sampleRate, int bitsPerSample, int bitrate, AudioCodecId codecId, CaptureAudioCallBack callback)
{
	return s_sessionManager.CreateCaptureSession(deviceIndex, 
		channels, 
		sampleRate,
		bitsPerSample,
		bitrate,
		codecId, 
		callback);
}

DLLSPEC HAUDIOSESSION __cdecl StartCaptureAudio(int deviceIndex, int channels, int sampleRate, int outputAdpcm, CaptureAudioCallBack callback)
{
	return StartCaptureAudioEx(deviceIndex, 
				channels, 
				sampleRate,
				16,
				0,
				(outputAdpcm != 0) ? AUDIOCODECID_ADPCM : AUDIOCODECID_PCM, 
				callback);
}

DLLSPEC void __cdecl StopCaptureAudio(HAUDIOSESSION session)
{
	s_sessionManager.DestroySession(session);
}

DLLSPEC int __cdecl SetAudioCaptureVolume(HAUDIOSESSION session, int volume)
{
	return s_sessionManager.SetAudioCaptureVolume(session, volume);
}

DLLSPEC int __cdecl GetAudioCaptureVolume(HAUDIOSESSION session)
{
	return s_sessionManager.GetAudioCaptureVolume(session);
}

DLLSPEC int __cdecl GetCaptureDeviceNum()
{
	return s_sessionManager.GetCaptureDeviceNum();
}

DLLSPEC int __cdecl GetCaptureDeviceName(int deviceIndex, char *buf, int len)
{
	return s_sessionManager.GetCaptureDeviceName(deviceIndex, buf, len);
}

DLLSPEC HAUDIOSESSION __cdecl StartPlaybackAmrAudio(int isWideBand)
{
	return StartPlaybackAudioEx(
								1, 
								(isWideBand == 0) ? 8000 : 16000,
								16,
								(isWideBand == 0) ? AUDIOCODECID_AMRNB : AUDIOCODECID_AMRWB);
}

DLLSPEC HAUDIOSESSION __cdecl StartCaptureAmrAudio(int deviceIndex, int isWideBand, int bitrate, CaptureAudioCallBack callback)
{
	return StartCaptureAudioEx(
								deviceIndex, 1, 
								(isWideBand == 0) ? 8000 : 16000,
								16,
								bitrate,
								(isWideBand == 0) ? AUDIOCODECID_AMRNB : AUDIOCODECID_AMRWB, 
								callback);
}

DLLSPEC int __cdecl PlaybackAmrAudioData(HAUDIOSESSION session, char *data, int len)
{
	return s_sessionManager.PlaybackAmrData(session, data, len);
}